<?php
namespace app\admin\controller;

//优惠券管理
class Coupon extends Common
{
	//优惠券列表
    public function coupon_list()
    {
		$where=array('a.c_id'=>array('gt',0));
        $name=Request()->param('name');
		//优惠券名称
		$status=Request()->param('status');
		//状态
        $type=Request()->param('type');
        //类型
        $start_time=strtotime(Request()->param('start_time'));
        $end_time=strtotime(Request()->param('end_time'));
        $page=Request()->param('page');
        $size=Request()->param('size');
        if($page<1){
            $page=1;
        }
        if($size<15){
            $size=15;
        }
        $now=($page-1)*$size;
		if($name!=''){
			$where['a.c_name']=array('like','%'.$name.'%');
		}
		if($status>0){
			if($status==1){
				$where['a.c_status']=1;
			}else{
				$where['a.c_status']=2;
			}
			
		}
        if($type>0){
            $where['a.c_type']=$type;
        }
        $where_time=array(array('gt',0),array('gt',0));
        if($start_time>0){
            $where_time[]=array('egt',$start_time);
        }else{
            $start_time='';
        }
        if($end_time>0){
            $where_time[]=array('elt',$end_time);
        }else{
            $end_time='';
        }
        $where['a.c_create_time']=$where_time;
        $coupon=db('coupon a')
			->order('a.c_sort desc')
			->where($where)
            ->limit("$now,$size")
			->select();
		//记录
        $count=db('coupon a')
            ->where($where)
            ->count();
        //等级信息
        $page_str=page($page,$size,$count,count($coupon));//分页
        $this->view->assign('page_str',$page_str);
		$this->view->assign('data',$coupon);
		$this->assign('status',$status);
        $this->assign('type',$type);
		$this->assign('name',$name);
        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);
        $this->assign('page',$page);
        $this->assign('size',$size);
        $this->assign('count',$count);
        return $this->view->fetch();

    }
	//添加优惠券
	public function add_coupon(){
		if(request()->isGet()){
			//get，加载页面
			return $this->view->fetch();
		}else if(request()->isAjax()){
			$data=request()->post();
            $data['c_name']=trim($data['c_name']);
			if($data['c_name']==''){
                return return_json(array('code'=>0,'msg'=>'请输入优惠券名称'),'添加优惠券');
            }
            if($data['c_discount']<1){
                return return_json(array('code'=>0,'msg'=>'优惠金额必须大于1'),'添加优惠券');
            }
            if($data['c_min_buy']<1 || $data['c_min_buy']<$data['c_discount']){
                return return_json(array('code'=>0,'msg'=>'最低消费金额必须大于优惠金额'),'添加优惠券');
            }
            if(strtotime($data['c_start_time'])<=0){
                return return_json(array('code'=>0,'msg'=>'请输入可用开始时间'),'添加优惠券');
            }
            if(strtotime($data['c_end_time'])+24*3600-1<=time()){
                return return_json(array('code'=>0,'msg'=>'可用结束时间必须在现在之后'),'添加优惠券');
            }
            $data['c_start_time']=strtotime($data['c_start_time']);//可用开始时间
            $data['c_end_time']=strtotime($data['c_end_time'])+24*3600-1;//可用结束时间
            if($data['c_type']==2 && $data['c_need_integral']<1){
                return return_json(array('code'=>0,'msg'=>'兑换积分必须大于1'),'添加优惠券');
            }
            if($data['c_type']==1 && $data['c_number']<1){
                return return_json(array('code'=>0,'msg'=>'一次赠送数量必须大于1'),'添加优惠券');
            }
            $coupon=db('coupon')->where(array('c_name'=>$data['c_name']))->find();
            if(!empty($coupon)){
                return return_json(array('code'=>0,'msg'=>'已有相同名称的优惠券'),'添加优惠券');
            }
            $data['c_create_time']=time();
            $data['c_add_user_id']=session('uid');
            $data['c_add_user_name']=session('user.real_name');
            $res=db('coupon')->insertGetId($data);
            //添加权限
            if($res>0){
                return return_json(array('code'=>1,'msg'=>'优惠券添加成功'),'添加优惠券');
            }else{
                return return_json(array('code'=>0,'msg'=>'优惠券添加失败'),'添加优惠券');
            }
		}
    }
    //禁用优惠券
    function coupon_del(){
        if(request()->isAjax()){
            $id=request()->post('id');
            $res=db('coupon')
                ->where(array('c_id'=>$id))
                ->update(array('c_status'=>2));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'禁用成功'),'禁用优惠券');
            }else{
                return return_json(array('code'=>0,'msg'=>'禁用失败'),'禁用优惠券');
            }
        }
    }
    //启用优惠券
    function coupon_use(){
        if(request()->isAjax()){
            $id=request()->post('id');
            $res=db('coupon')
                ->where(array('c_id'=>$id))
                ->update(array('c_status'=>1));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'启用成功'),'启用优惠券');
            }else{
                return return_json(array('code'=>0,'msg'=>'启用失败'),'启用优惠券');
            }
        }
    }
    //单人赠送
    function coupon_give(){
        if(request()->isGet()){
            //get，加载页面
            $id=request()->get('id');
            $coupon=db('coupon')
                ->where(array('c_id'=>$id))
                ->find();
            $this->assign('coupon',$coupon);
            return $this->view->fetch();
        }else if(request()->isAjax()) {
            $data = request()->post();
            $mobile=trim($data['mobile']);
            $coupon=db('coupon')
                ->where(array('c_id'=>$data['id'],'c_status'=>1))
                ->find();
            if(empty($coupon)){
                return return_json(array('code'=>0,'msg'=>'没有该优惠券或已冻结'),'单人赠送优惠券');
            }
            if($coupon['c_end_time']<time()){
                return return_json(array('code'=>0,'msg'=>'优惠券已过期，无法发放'),'单人赠送优惠券');
            }
            $user=db('user a')
                ->where(array('u_mobile'=>$mobile,'u_status'=>1))
                ->find();
            if(empty($user)){
                return return_json(array('code'=>0,'msg'=>'没有该用户或冻结中'),'单人赠送优惠券');
            }
            if($data['num']<1){
                return return_json(array('code'=>0,'msg'=>'发放数量必须大于1'),'单人赠送优惠券');
            }
            for($i=1;$i<=$data['num'];$i++){
                $data1=array('u_id'=>$user['u_id'],'c_id'=>$coupon['c_id'],'uc_discount'=>$coupon['c_discount'],'uc_min_buy'=>$coupon['c_min_buy'],'uc_create_time'=>time(),'uc_start_time'=>$coupon['c_start_time'],'uc_end_time'=>$coupon['c_end_time'],'uc_type'=>3,'uc_status'=>1);
                $res=db('user_coupon')
                    ->insertGetId($data1);
            }
            $data2=array('ucl_create_time'=>time(),'uc_id'=>$res,'u_id'=>$user['u_id'],'c_id'=>$coupon['c_id'],'c_num'=>$data['num'],'c_type'=>3);
            $res1=db('user_coupon_log')
                ->insertGetId($data2);
            $res2=db('coupon')
                ->where(array('c_id'=>$data['id'],'c_status'=>1))
                ->setInc('u_grant_number',$data['num']);
            if($res>0 && $res1>0 && $res2!==false){
                return return_json(array('code'=>1,'msg'=>'发放成功'),'单人赠送优惠券');
            }else{
                return return_json(array('code'=>0,'msg'=>'发放失败'),'单人赠送优惠券');
            }
        }
    }
    //多人赠送
    function coupon_gives(){
        if(request()->isGet()){
            //get，加载页面
            $id=request()->get('id');
            $coupon=db('coupon')
                ->where(array('c_id'=>$id))
                ->find();
            $this->assign('coupon',$coupon);
            return $this->view->fetch();
        }else if(request()->isAjax()) {
            $data = request()->post();
            $mobiles=explode("|",trim($data['mobiles']));
            if(empty($mobiles)){
                return return_json(array('code'=>0,'msg'=>'请输入用户注册手机号'),'多人赠送优惠券');
            }
            $coupon=db('coupon')
                ->where(array('c_id'=>$data['id'],'c_status'=>1))
                ->find();
            if(empty($coupon)){
                return return_json(array('code'=>0,'msg'=>'没有该优惠券或已冻结'),'多人赠送优惠券');
            }
            if($coupon['c_end_time']<time()){
                return return_json(array('code'=>0,'msg'=>'优惠券已过期，无法发放'),'多人赠送优惠券');
            }
            foreach($mobiles as $k=>$v){
                $user=db('user a')
                    ->where(array('u_mobile'=>$v,'u_status'=>1))
                    ->find();
                if(!empty($user)){
                    $data1=array('u_id'=>$user['u_id'],'c_id'=>$coupon['c_id'],'uc_discount'=>$coupon['c_discount'],'uc_min_buy'=>$coupon['c_min_buy'],'uc_create_time'=>time(),'uc_start_time'=>$coupon['c_start_time'],'uc_end_time'=>$coupon['c_end_time'],'uc_type'=>3,'uc_status'=>1);
                    $res=db('user_coupon')
                        ->insertGetId($data1);
                    $data2=array('ucl_create_time'=>time(),'uc_id'=>$res,'u_id'=>$user['u_id'],'c_id'=>$coupon['c_id'],'c_num'=>1,'c_type'=>3);
                    $res1=db('user_coupon_log')
                        ->insertGetId($data2);
                    $res2=db('coupon')
                        ->where(array('c_id'=>$data['id'],'c_status'=>1))
                        ->setInc('u_grant_number');
                }
            }
            if($res>0 && $res1>0 && $res2!==false){
                return return_json(array('code'=>1,'msg'=>'发放成功'),'多人赠送优惠券');
            }else{
                return return_json(array('code'=>0,'msg'=>'发放失败'),'多人赠送优惠券');
            }
        }
    }
}
